package controllers

import (
	"gosync/models"
	"strings"
	"gosync/utils"
	"github.com/astaxie/beego"
)

type ProjectController struct {
	BaseController
}





/*
项目管理
 */
func (this *ProjectController) List(){
	mc := models.Machine{}
	allmc := mc.AllList()
	alljson := utils.JsonEncode(allmc)


	beego.Notice(alljson)
	this.Data["alljson"] = alljson
	this.setTpl("project_list.html")
}

/*
数据列表接口
 */
 func (this *ProjectController) ListApi(){
 	page, _ := this.GetInt("page")
 	pjRes := models.Project{}
 	datas, total := pjRes.PageList(page)

 	this.pageApiJson(datas, total, "", 0)
 }

 /*
 查询绑定IP的数据
  */
  func (this *ProjectController) GetBindIp(){
	  id, _ := this.GetInt("id")
	  if id==0{
		  this.ajaxReturn("", "项目ID丢失", 400)
	  }

	  pjmodel := models.Project{}
	  ips,_ := pjmodel.GetBindIp(id)

	  this.ajaxReturn(ips, "成功", 200)
  }

/*
添加项目
 */
 func (this *ProjectController) AddPj(){

	this.setTpl("project_add.html")
 }

 /*
 编辑项目
  */
  func (this *ProjectController) EditPj(){
	  id, _ := this.GetInt("id")
	  if id==0{
		  this.goError("ID丢失！")
	  }
	  model := models.Project{}
	  info, _ := model.GetOne(id)

	  var show_exclude string
	  if info.Exclude != ""{
		  var_interface := utils.JsonDecode(info.Exclude)
		  exclude := var_interface.([]interface{})

		  length := len(exclude)

		  for key, item:=range exclude{
			  node,_ := item.(string)
			  if length-key <=1 {
				  show_exclude += node
			  }else{
				  show_exclude += node+"\r\n"
			  }

		  }
	  }else{
		  show_exclude = ""
	  }



	  this.Data["info"] = info
	  this.Data["exclude"] = show_exclude


	  this.setTpl("project_edit.html")
  }

  func (this *ProjectController) EditPjDo(){
	  id, _ := this.GetInt("id")
	  if id==0{
		  this.ajaxReturn("", "ID丢失", 400)
	  }
	  name := strings.TrimSpace(this.GetString("name"))
	  account := strings.TrimSpace(this.GetString("account"))
	  from_path := strings.TrimSpace(this.GetString("from_path"))
	  exclude := strings.TrimSpace(this.GetString("exclude"))



	  var save_exclude string
	  if exclude != ""{
		  save_exclude = utils.JsonEncode(strings.Split(exclude, "\r\n"))
	  }else{
		  save_exclude = ""
	  }
	  params := models.Project{
	  	Id:id,
	  	Name:name,
	  	Account:account,
	  	From_path:from_path,
	  	Exclude:save_exclude,
	  }


	  result, err := params.EditPj()
		beego.Notice(err)
	  if result <= 0 {
		  this.ajaxReturn("", "失败", 400)
	  }else{
		  this.ajaxReturn(map[string]string{"url":this.URLFor("ProjectController.List")}, "成功", 200)
	  }
  }



/*
添加项目操作
 */
 func (this *ProjectController) AddPjDo(){
	 name := strings.TrimSpace(this.GetString("name"))
	 account := strings.TrimSpace(this.GetString("account"))
	 from_path := strings.TrimSpace(this.GetString("from_path"))
	 exclude := strings.TrimSpace(this.GetString("exclude"))

	var save_exclude string
	 if exclude != ""{
	 	save_exclude = utils.JsonEncode(strings.Split(exclude, "\r\n"))
	 }else{
	 	save_exclude = ""
	 }

	 params := models.Project{
		 Name:name,
		 Account:account,
		 From_path:from_path,
		 Exclude:save_exclude,
	 }
	 result := params.AddPj()

	 if result != nil {
		 error_msg := result.Error()
		 this.ajaxReturn("", "添加失败："+error_msg, 400)
	 }else{
		 this.ajaxReturn(map[string]string{"url":this.URLFor("ProjectController.List")}, "成功", 200)
	 }
 }


 /*
 删除项目
  */

  func (this *ProjectController) DelPj(){
  	id, _ := this.GetInt("id")

	model := models.Project{}
  	_, err := model.DelPj(id)

	  if err != nil {
		  this.ajaxReturn("", "删除失败", 400)
	  }else{
		  this.ajaxReturn("", "成功", 200)
	  }
  }



  /*
  关联IP
   */
func (this *ProjectController) BindIp(){
	var ips []string
	project_id, _ := this.GetInt("project_id")
	this.Ctx.Input.Bind(&ips, "bind_mc")  //接受数组类型参数
	if project_id <=0{
		this.ajaxReturn("", "ID异常", 400)
	}
	if len(ips) <=0{
		this.ajaxReturn("", "未关联任何机器", 400)
	}
	pjmodel := models.Project{}
	err := pjmodel.BindIp(project_id, ips)

	if err != nil {
		this.ajaxReturn("", "删除失败", 400)
	}else{
		this.ajaxReturn("", "成功", 200)
	}
}